n <- length(y)
y_centered <- y - mean_y
variance_y <- sum(y_centered^2) / n
cat("Variance (diviseur n) :", variance_y, "\n")
# 2.1.1 Calcul manuel de la fonction d'autocovariance et d'autocorrélation
lag_max <- 10
acvf <- numeric(lag_max + 1)
acf_vals <- numeric(lag_max + 1)
lags <- 0:lag_max
# Lag 0
acvf[1] <- variance_y
acf_vals[1] <- 1.0
# Calcul pour les retards 1 à lag_max
for (k in 1:lag_max) {
gamma_k <- sum(y_centered[1:(n - k)] * y_centered[(k + 1):n]) / n
acvf[k + 1] <- gamma_k
acf_vals[k + 1] <- gamma_k / variance_y
}
# Affichage des premières valeurs dans la console
cat("\n--- Fonction d'autocovariance (ACVF) et d'autocorrélation (ACF) ---\n")
cat("Retard |   ACVF    |    ACF   \n")
for (k in 0:min(10, lag_max)) {
cat(sprintf("  %2d   | %8.6f | %8.4f\n", k, acvf[k+1], acf_vals[k+1]))
}
# 2.2 Distribution : Histogramme + Densité
dens <- density(y)
ymax <- max(hist(y, plot = FALSE)$density, max(density(y)$y)) * 1.15
hist(y, prob = TRUE, main = paste("Distribution des rentabilités :", symbole),
xlab = "Log-rentabilité",
col = "lightblue",
border = "black",
ylim = c(0, ymax))
lines(density(y), col = "red", lwd = 2)
# 2.3 Boxplot
boxplot(y, main = paste("Boxplot des rentabilités :", symbole), ylab = "Log-rentabilité", col = "blue", horizontal = TRUE)
# 2.4 Autocorrelogramme
ci_95 <- 1.96 / sqrt(n)
plot(lags, acf_vals, type = "h", lwd = 2, col = "blue",
main = "Autocorrelogramme des rentabilités",
xlab = expression(k),
ylab = expression(gamma[k]),
ylim = c(min(-1, min(acf_vals) - 0.1), max(1, max(acf_vals) + 0.1)))
abline(h = 0, col = "black", lwd = 1)
abline(h = ci_95, col = "red", lty = 2, lwd = 1.5)
abline(h = -ci_95, col = "red", lty = 2, lwd = 1.5)
legend("topright",
legend = c("Valeurs ACF", "IC 95% (Bruit blanc)"),
col = c("blue", "red"),
lty = c(1, 2),
lwd = c(2, 1.5),
bg = "white",
cex = 0.8)
# 1. SIMULATION DES DONNEES
#set.seed(123)
theta <- 2
alphas <- c(-0.5)
betas <- c(0.2)
nb <- 1000
modele <- list(ar = alphas, ma = betas)
y_centred <- arima.sim(model = modele, n = nb, sd = sqrt(4))
y <- theta + y_centred
y_ts <- ts(y)
# 1.4 Visualiser la série temporelle simulée avec Moyenne et IC 95%
mean_y <- mean(y)
sd_y <- sd(y)
# Création du graphique de base
plot(y_ts, main = "Données simulée",
xlab =  expression(t),
ylab = expression(y[t]),
col = "blue",
type = "l",
lwd = 0.8)
# Ajout des lignes horizontales (Moyenne et IC 95%)
abline(h = mean_y, col = "red", lwd = 2)
abline(h = mean_y + 1.96 * sd_y, col = "darkred", lty = 2, lwd = 1.5)
abline(h = mean_y - 1.96 * sd_y, col = "darkred", lty = 2, lwd = 1.5)
# Légende pour clarifier les lignes
legend("topright",
legend = c("Série simulée", "Moyenne", "IC 95% (Moyenne ± 1.96*SD)"),
col = c("blue", "red", "darkred"),
lty = c(1, 1, 2),
lwd = c(0.8, 2, 1.5),
cex = 0.6,
bg = "white")
# 2. ANALYSE EXPLORATOIRE DES DONNEES
# 2.1 Statistiques descriptives
cat("\n--- Statistiques descriptives ---\n")
print(summary(y))
# Calcul de la variance
n <- length(y)
y_centered <- y - mean_y
variance_y <- sum(y_centered^2) / n
cat("Variance (diviseur n) :", variance_y, "\n")
# 2.1.1 Calcul manuel de la fonction d'autocovariance  et d'autocorrélation
lag_max <- 10
acvf <- numeric(lag_max + 1)
acf_vals <- numeric(lag_max + 1)
lags <- 0:lag_max
# Lag 0
acvf[1] <- variance_y
acf_vals[1] <- 1.0
# Calcul pour les retards 1 à lag_max
for (k in 1:lag_max) {
# Autocovariance au retard k : moyenne des produits des écarts décalés
gamma_k <- sum(y_centered[1:(n - k)] * y_centered[(k + 1):n]) / n
acvf[k + 1] <- gamma_k
# Autocorrélation : autocovariance normalisée par la variance
acf_vals[k + 1] <- gamma_k / variance_y
}
# Affichage des premières valeurs dans la console
cat("\n--- Fonction d'autocovariance (ACVF) et d'autocorrélation (ACF) ---\n")
cat("Retard |   ACVF    |    ACF   \n")
for (k in 0:min(10, lag_max)) {
cat(sprintf("  %2d   | %8.4f | %8.4f\n", k, acvf[k+1], acf_vals[k+1]))
}
# 2.2 Distribution : Histogramme + Densité
# Calcul préalable de la densité pour déterminer les limites appropriées
dens <- density(y)
ymax <- max(dens$y) * 1.1  # Ajoute une marge de 10%
hist(y, prob = TRUE, main = "Distribution statistique", xlab = "y",
col = "lightblue", border = "black",
ylim = c(0, ymax))  # Limite Y ajustée pour afficher tout le sommet
lines(dens, col = "red", lwd = 2)  # Utilisation de l'objet dens déjà calculé
# 2.3 Boxplot
boxplot(y, main = "Boxplot", ylab = "y", col = "blue", horizontal = TRUE)
# 2.4 Autocorrelogramme
ci_95 <- 1.96 / sqrt(n)
# Tracé de l'ACF avec type = "h" (lignes verticales de haute densité, standard pour les correlogrammes)
plot(lags, acf_vals, type = "h", lwd = 2, col = "blue",
main = "Autocorrelogramme",
xlab = expression(k),
ylab = expression(gamma[k]),
ylim = c(min(-1, min(acf_vals) - 0.1), max(1, max(acf_vals) + 0.1)))
# Ligne horizontale à 0
abline(h = 0, col = "black", lwd = 1)
# Lignes d'intervalle de confiance à 95% (hypothèse de bruit blanc)
abline(h = ci_95, col = "red", lty = 2, lwd = 1.5)
abline(h = -ci_95, col = "red", lty = 2, lwd = 1.5)
# Légende
legend("topright",
legend = c("Valeurs ACF", "IC 95% (Bruit blanc)"),
col = c("blue", "red"),
lty = c(1, 2),
lwd = c(2, 1.5),
bg = "white",
cex = 0.8)
#===============================================================================
# 3. BONUS: COMPARAISON : VALEURS THÉORIQUES VS VALEURS ESTIMÉES
#===============================================================================
# Récupération des paramètres du modèle simulé
phi <- alphas[1]       # Coefficient AR = 0.5
theta_ma <- betas[1]   # Coefficient MA = 0.2
sigma2 <- 4            # Variance du bruit blanc (sd^2)
mu_true <- theta       # Moyenne vraie = 2
# --- A. CALCUL DES VALEURS THÉORIQUES ---
# 1. Moyenne théorique
mu_theo <- mu_true
# 2. Variance théorique (Gamma_0)
# Formule : sigma^2 * (1 + 2*phi*theta + theta^2) / (1 - phi^2)
gamma0_theo <- sigma2 * (1 + 2 * phi * theta_ma + theta_ma^2) / (1 - phi^2)
# 3. Autocovariance théorique (Gamma_k)
# Gamma_1 = sigma^2 * (1 + phi*theta) * (phi + theta) / (1 - phi^2)
gamma1_theo <- sigma2 * (1 + phi * theta_ma) * (phi + theta_ma) / (1 - phi^2)
# Pour k >= 2, Gamma_k = phi * Gamma_{k-1}
# 4. Autocorrélation théorique (Rho_k)
rho0_theo <- 1.0
rho1_theo <- gamma1_theo / gamma0_theo
# Pour k >= 2, Rho_k = phi * Rho_{k-1}
# --- B. RÉCUPÉRATION DES VALEURS ESTIMÉES (EMPIRIQUES) ---
mu_est <- mean(y)
var_est <- var(y)
# --- C. AFFICHAGE DES TABLEAUX DE COMPARAISON ---
cat("\n===============================================================================\n")
cat("       COMPARAISON : VALEURS THÉORIQUES VS VALEURS ESTIMÉES (Échantillon n=", n, ")\n", sep="")
cat("===============================================================================\n")
# 1. Tableau Moyenne et Variance
df_moments <- data.frame(
Statistique = c("Moyenne", "Variance"),
Theorique = c(mu_theo, gamma0_theo),
Estime = c(mu_est, var_est),
Erreur_Absolue = c(abs(mu_theo - mu_est), abs(gamma0_theo - var_est))
)
# Formatage pour un affichage propre
df_moments$Theorique <- round(df_moments$Theorique, 4)
df_moments$Estime <- round(df_moments$Estime, 4)
df_moments$Erreur_Absolue <- round(df_moments$Erreur_Absolue, 4)
cat("\n--- 1. Moments d'ordre 1 et 2 ---\n")
print(df_moments, row.names = FALSE)
# 2. Tableau ACVF et ACF (pour les retards 0 à 5)
lag_max_comp <- 5
lags_comp <- 0:lag_max_comp
# Calcul théorique pour ces retards spécifiques
acvf_theo_vec <- numeric(lag_max_comp + 1)
acf_theo_vec <- numeric(lag_max_comp + 1)
acvf_theo_vec[1] <- gamma0_theo
acf_theo_vec[1] <- rho0_theo
if (lag_max_comp >= 1) {
acvf_theo_vec[2] <- gamma1_theo
acf_theo_vec[2] <- rho1_theo
}
for (k in 2:lag_max_comp) {
acvf_theo_vec[k+1] <- phi * acvf_theo_vec[k]
acf_theo_vec[k+1] <- phi * acf_theo_vec[k]
}
# Extraction des valeurs estimées calculées précédemment
acvf_est_vec <- acvf[1:(lag_max_comp + 1)]
acf_est_vec <- acf_vals[1:(lag_max_comp + 1)]
# Création des dataframes de comparaison
df_acvf <- data.frame(
Retard = lags_comp,
ACVF_Theorique = round(acvf_theo_vec, 4),
ACVF_Estimee = round(acvf_est_vec, 4),
Diff_Absolue = round(abs(acvf_theo_vec - acvf_est_vec), 4)
)
df_acf <- data.frame(
Retard = lags_comp,
ACF_Theorique = round(acf_theo_vec, 4),
ACF_Estimee = round(acf_est_vec, 4),
Diff_Absolue = round(abs(acf_theo_vec - acf_est_vec), 4)
)
cat("\n--- 2. Fonction d'Autocovariance (ACVF) ---\n")
print(df_acvf, row.names = FALSE)
cat("\n--- 3. Fonction d'Autocorrélation (ACF) ---\n")
print(df_acf, row.names = FALSE)
# 1. SIMULATION DES DONNEES
#set.seed(123)
theta <- 2
alphas <- c(-0.5)
betas <- c(0.2)
nb <- 1000
nb <- 1000
modele <- list(ar = alphas, ma = betas)
# 1. SIMULATION DES DONNEES
#set.seed(123)
theta <- 2
alphas <- c(-0.5)
betas <- c(0.2)
sig2eps <- 4
nb <- 1000
modele <- list(ar = alphas, ma = betas)
y_centred <- arima.sim(model = modele, n = nb, sd = sqrt(sig2eps))
y <- theta + y_centred
y_ts <- ts(y)
y_ts
y
# 1.4 Visualiser la série temporelle simulée avec Moyenne et IC 95%
mean_y <- mean(y)
sd_y <- sd(y)
# Création du graphique de base
plot(y_ts, main = "Données simulée",
xlab =  expression(t),
ylab = expression(y[t]),
col = "blue",
type = "l",
lwd = 0.8)
# Création du graphique de base
plot(y_ts, main = "Données simulées",
xlab =  expression(t),
ylab = expression(y[t]),
col = "blue",
type = "l",
lwd = 0.8)
# Ajout des lignes horizontales (Moyenne et IC 95%)
abline(h = mean_y, col = "red", lwd = 2)
abline(h = mean_y + 1.96 * sd_y, col = "darkred", lty = 2, lwd = 1.5)
abline(h = mean_y - 1.96 * sd_y, col = "darkred", lty = 2, lwd = 1.5)
# Légende pour clarifier les lignes
legend("topright",
legend = c("Série simulée", "Moyenne", "IC 95% (Moyenne ± 1.96*SD)"),
col = c("blue", "red", "darkred"),
lty = c(1, 1, 2),
lwd = c(0.8, 2, 1.5),
cex = 0.6,
bg = "white")
# 2.1 Statistiques descriptives
cat("\n--- Statistiques descriptives ---\n")
print(summary(y))
# Calcul de la variance
n <- length(y)
y_centered <- y - mean_y
variance_y <- sum(y_centered^2) / n
cat("Variance (diviseur n) :", variance_y, "\n")
# 2.1.1 Calcul manuel de la fonction d'autocovariance  et d'autocorrélation
lag_max <- 10
acvf <- numeric(lag_max + 1)
acf_vals <- numeric(lag_max + 1)
lags <- 0:lag_max
# Lag 0
acvf[1] <- variance_y
acf_vals[1] <- 1.0
# Calcul pour les retards 1 à lag_max
for (k in 1:lag_max) {
# Autocovariance au retard k : moyenne des produits des écarts décalés
gamma_k <- sum(y_centered[1:(n - k)] * y_centered[(k + 1):n]) / n
acvf[k + 1] <- gamma_k
# Autocorrélation : autocovariance normalisée par la variance
acf_vals[k + 1] <- gamma_k / variance_y
}
# Affichage des premières valeurs dans la console
cat("\n--- Fonction d'autocovariance (ACVF) et d'autocorrélation (ACF) ---\n")
cat("Retard |   ACVF    |    ACF   \n")
for (k in 0:min(10, lag_max)) {
cat(sprintf("  %2d   | %8.4f | %8.4f\n", k, acvf[k+1], acf_vals[k+1]))
}
# Calcul pour les retards 1 à lag_max
for (k in 1:lag_max) {
# Autocovariance au retard k : moyenne des produits des écarts décalés
gamma_k <- sum(y_centered[1:(n - k)] * y_centered[(k + 1):n]) / n
acvf[k + 1] <- gamma_k
# Autocorrélation : autocovariance normalisée par la variance
acf_vals[k + 1] <- gamma_k / variance_y
}
# Affichage des premières valeurs dans la console
cat("\n--- Fonction d'autocovariance (ACVF) et d'autocorrélation (ACF) ---\n")
cat("Retard |   ACVF    |    ACF   \n")
for (k in 0:min(10, lag_max)) {
cat(sprintf("  %2d   | %8.4f | %8.4f\n", k, acvf[k+1], acf_vals[k+1]))
}
# 2.2 Distribution : Histogramme + Densité
# Calcul préalable de la densité pour déterminer les limites appropriées
dens <- density(y)
ymax <- max(dens$y) * 1.1  # Ajoute une marge de 10%
hist(y, prob = TRUE, main = "Distribution statistique", xlab = "y",
col = "lightblue", border = "black",
ylim = c(0, ymax))  # Limite Y ajustée pour afficher tout le sommet
print(variance_y)
# 1. SIMULATION DES DONNEES
#set.seed(123)
theta <- 2
alphas <- c(-0.5)
betas <- c(0.2)
sig2eps <- 4
nb <- 1000
modele <- list(ar = alphas, ma = betas)
y_centred <- arima.sim(model = modele, n = nb, sd = sqrt(sig2eps))
y <- theta + y_centred
y_ts <- ts(y)
# 1.4 Visualiser la série temporelle simulée avec Moyenne et IC 95%
mean_y <- mean(y)
sd_y <- sd(y)
# Création du graphique de base
plot(y_ts, main = "Données simulées",
xlab =  expression(t),
ylab = expression(y[t]),
col = "blue",
type = "l",
lwd = 0.8)
# Ajout des lignes horizontales (Moyenne et IC 95%)
abline(h = mean_y, col = "red", lwd = 2)
abline(h = mean_y + 1.96 * sd_y, col = "darkred", lty = 2, lwd = 1.5)
abline(h = mean_y - 1.96 * sd_y, col = "darkred", lty = 2, lwd = 1.5)
# Légende pour clarifier les lignes
legend("topright",
legend = c("Série simulée", "Moyenne", "IC 95% (Moyenne ± 1.96*SD)"),
col = c("blue", "red", "darkred"),
lty = c(1, 1, 2),
lwd = c(0.8, 2, 1.5),
cex = 0.6,
bg = "white")
# 2.1 Statistiques descriptives
print(summary(y))
# Calcul de la variance
n <- length(y)
y_centered <- y - mean_y
variance_y <- sum(y_centered^2) / n
print(variance_y)
# 2.1.1 Fonctions d'autocovariance  et d'autocorrélation
lag_max <- 10
acvf <- numeric(lag_max + 1)
acf_vals <- numeric(lag_max + 1)
lags <- 0:lag_max
# Lag k = 0
acvf[1] <- variance_y
acf_vals[1] <- 1.0
# Calcul pour les retards k de 1 à lag_max
for (k in 1:lag_max) {
# Autocovariance :
gamma_k <- sum(y_centered[1:(n - k)] * y_centered[(k + 1):n]) / n
acvf[k + 1] <- gamma_k
# Autocorrélation :
acf_vals[k + 1] <- gamma_k / variance_y
}
# Affichage des premières valeurs
cat("\n--- Fonction d'autocovariance (ACVF) et d'autocorrélation (ACF) ---\n")
cat("Retard |   ACVF    |    ACF   \n")
for (k in 0:min(10, lag_max)) {
cat(sprintf("  %2d   | %8.4f | %8.4f\n", k, acvf[k+1], acf_vals[k+1]))
}
# 2.2 Distribution : Histogramme + Densité
dens <- density(y)
ymax <- max(dens$y) * 1.1
hist(y, prob = TRUE, main = "Distribution statistique", xlab = "y",
col = "lightblue", border = "black",
ylim = c(0, ymax))
lines(dens, col = "red", lwd = 2)
# 2.3 Boxplot
boxplot(y, main = "Boxplot", ylab = "y", col = "blue", horizontal = TRUE)
# 2.4 Autocorrelogramme
ci_95 <- 1.96 / sqrt(n)
# Tracé de l'ACF
plot(lags, acf_vals, type = "h", lwd = 2, col = "blue",
main = "Autocorrelogramme",
xlab = expression(k),
ylab = expression(gamma[k]),
ylim = c(min(-1, min(acf_vals) - 0.1), max(1, max(acf_vals) + 0.1)))
# Ligne horizontale à 0
abline(h = 0, col = "black", lwd = 1)
# Lignes d'intervalle de confiance à 95% (hypothèse de bruit blanc)
abline(h = ci_95, col = "red", lty = 2, lwd = 1.5)
abline(h = -ci_95, col = "red", lty = 2, lwd = 1.5)
# Légende
legend("topright",
legend = c("Valeurs ACF", "IC 95% (Bruit blanc)"),
col = c("blue", "red"),
lty = c(1, 2),
lwd = c(2, 1.5),
bg = "white",
cex = 0.8)
# 0. CHARGEMENT DU PACKAGE POUR LE TÉLÉCHARGEMENT
# install.packages("quantmod") # Décommentez si nécessaire
library(quantmod)
# 0. CHARGEMENT DU PACKAGE POUR LE TÉLÉCHARGEMENT
# install.packages("quantmod") # Décommentez si nécessaire
library(quantmod)
# 1. RÉCUPÉRATION DES DONNÉES RÉELLES (Log-rentabilités)
symbole <- "AAPL"
# Téléchargement des données
getSymbols(symbole, src = "yahoo", from = "2020-01-01", auto.assign = TRUE)
# Extraction du prix ajusté à la clôture
prix <- Ad(get(symbole))
# Calcul des log-rentabilités : ln(P_t) - ln(P_{t-1})
y_log_ret <- diff(log(prix))
# Suppression de la première valeur (qui est NA à cause de la fonction diff)
y <- as.numeric(na.omit(y_log_ret))
y_ts <- ts(y)
# 1.4 Visualiser la série temporelle avec Moyenne et IC 95%
mean_y <- mean(y)
sd_y <- sd(y)
# Création du graphique de base
plot(y_ts, main = paste("Log-rentabilités journalières :", symbole),
xlab = expression(t),
ylab = expression(r[t]), # r pour rendement
col = "blue",
type = "l",
lwd = 0.8)
# Ajout des lignes horizontales (Moyenne et IC 95%)
abline(h = mean_y, col = "red", lwd = 2)
abline(h = mean_y + 1.96 * sd_y, col = "darkred", lty = 2, lwd = 1.5)
abline(h = mean_y - 1.96 * sd_y, col = "darkred", lty = 2, lwd = 1.5)
# Légende
legend("topright",
legend = c("Rentabilités", "Moyenne", "IC 95% (Moyenne ± 1.96*SD)"),
col = c("blue", "red", "darkred"),
lty = c(1, 1, 2),
lwd = c(0.8, 2, 1.5),
cex = 0.7,
bg = "white")
# 2.1 Statistiques descriptives
print(summary(y))
# Calcul de la variance
n <- length(y)
y_centered <- y - mean_y
variance_y <- sum(y_centered^2) / n
print(variance_y)
# 2.1.1 Fonctions d'autocovariance et d'autocorrélation
lag_max <- 10
acvf <- numeric(lag_max + 1)
acf_vals <- numeric(lag_max + 1)
lags <- 0:lag_max
# Lag k = 0
acvf[1] <- variance_y
acf_vals[1] <- 1.0
# Calcul pour les retards k de 1 à lag_max
for (k in 1:lag_max) {
gamma_k <- sum(y_centered[1:(n - k)] * y_centered[(k + 1):n]) / n
acvf[k + 1] <- gamma_k
acf_vals[k + 1] <- gamma_k / variance_y
}
# Affichage des premières valeurs
cat("\n--- Fonction d'autocovariance (ACVF) et d'autocorrélation (ACF) ---\n")
cat("Retard |   ACVF    |    ACF   \n")
for (k in 0:min(10, lag_max)) {
cat(sprintf("  %2d   | %8.6f | %8.4f\n", k, acvf[k+1], acf_vals[k+1]))
}
# 2.2 Distribution : Histogramme + Densité
dens <- density(y)
ymax <- max(hist(y, plot = FALSE)$density, max(density(y)$y)) * 1.15
hist(y, prob = TRUE, main = paste("Distribution des rentabilités :", symbole),
xlab = "Log-rentabilité",
col = "lightblue",
border = "black",
ylim = c(0, ymax))
lines(density(y), col = "red", lwd = 2)
# 2.3 Boxplot
boxplot(y, main = paste("Boxplot des rentabilités :", symbole), ylab = "Log-rentabilité", col = "blue", horizontal = TRUE)
# 2.4 Autocorrelogramme
ci_95 <- 1.96 / sqrt(n)
plot(lags, acf_vals, type = "h", lwd = 2, col = "blue",
main = "Autocorrelogramme des rentabilités",
xlab = expression(k),
ylab = expression(gamma[k]),
ylim = c(min(-1, min(acf_vals) - 0.1), max(1, max(acf_vals) + 0.1)))
abline(h = 0, col = "black", lwd = 1)
abline(h = ci_95, col = "red", lty = 2, lwd = 1.5)
abline(h = -ci_95, col = "red", lty = 2, lwd = 1.5)
legend("topright",
legend = c("Valeurs ACF", "IC 95% (Bruit blanc)"),
col = c("blue", "red"),
lty = c(1, 2),
lwd = c(2, 1.5),
bg = "white",
cex = 0.8)
